<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" destroyOnClose showFooter title="退卡申请" :width="drawerWith" @ok="handleSubmit">
    <p style="color:red">此会员消费明细仅供参考，具体以实际消费核实为准</p>
    <Description size="middle" class="mt-4" title="基础信息" :column="{ xxl: 4, xl: 3, lg: 3, md: 3, sm: 2, xs: 1 }" :data="userGoodsInfo.data" :schema="userGoodsInfoSchema" />
    <p style="color:red">请填写会员实际承担金额，系统将自动计算并生成对应退款金额</p>
    <BasicForm @register="registerForm"></BasicForm>
  </BasicDrawer>
</template>
<script lang="ts" setup>
import { reactive, ref, unref } from 'vue';
import { BasicForm, useForm } from '@apps/antd-admin/src/components/Form/index';
import { BasicDrawer, useDrawerInner } from '@apps/antd-admin/src/components/drawer';
import { backCardFormSchema } from '../data/data';
import { DescItem, Description } from '@apps/antd-admin/src/components/description';
import { backcardSub, getUserGoodsConsumptionInfo } from '../data/api';
import { useAppInject } from '@apps/antd-admin/src/hooks/web/use-app-inject';
import { render } from '@apps/antd-admin/src/hooks/render/renderUtils';

const { getIsMobile } = useAppInject();
const drawerWith = ref('40%');
if (unref(getIsMobile)) {
  drawerWith.value = '80%';
}
// 声明Emits
const emit = defineEmits(['success', 'register']);
const isUpdate = ref(true);
let userGoodsInfo: Recordable = reactive({ data: {} });

const [registerForm, { resetFields, setFieldsValue, validate, updateSchema }] = useForm({
  labelWidth: 150,
  labelCol: { span: 4 },
  schemas: backCardFormSchema,
  showActionButtonGroup: false,
});

const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async data => {
  resetFields();
  setDrawerProps({ confirmLoading: false });
  isUpdate.value = !!data?.isUpdate;
  if (unref(isUpdate)) {
    getUserGoodsConsumptionInfo({
      userGoodsId: data.record.userGoodsId,
    }).then(result => {
      result.eleUseBalance = '去票务系统核算';
      userGoodsInfo.data = result;
      let record = data.record;
      let isUpgrade = userGoodsInfo.data.isUpgrade;
      if (isUpgrade == 1) {
        record.payAmount = userGoodsInfo.data.upgradePayAmount;
      }
      let canRefundOriginal = userGoodsInfo.data.canRefundOriginal;
      let refundAmount = record.payAmount - 0;
      let formData = {
        userGoodsId: record.userGoodsId,
        canRefundOriginal: 0,
        isUpgrade: isUpgrade,
        payAmount: record.payAmount,
        refundAmount: refundAmount,
        refundWay: 'refund_bank',
        bearWay: 1,
        vipBearAmount: 0,
        saleBearAmount: 0,
        refundReason: '',
        remark: '',
      };
      if (canRefundOriginal) {
        formData.refundWay = 'refund_original';
        formData.canRefundOriginal = 1;
      } else {
        formData.refundWay = 'refund_bank';
      }
      setFieldsValue({
        ...formData,
      });
    });
  }
});

const userGoodsInfoSchema: DescItem[] = [
  {
    field: 'userName',
    label: '会员姓名',
    span: 3,
  },
  {
    field: 'mobile',
    label: '手机号',
    span: 3,
  },
  {
    field: 'vipCardNo',
    label: '会员卡号',
    span: 3,
  },
  {
    field: 'buyDate',
    label: '开卡日期',
    span: 3,
  },
  {
    field: 'airportName',
    label: '所属机场',
    span: 3,
  },
  {
    field: 'employeeName',
    label: '销售',
    span: 3,
  },
  {
    field: 'goodsName',
    label: '卡种',
    span: 3,
  },
  {
    field: 'payAmount',
    label: '支付金额',
    span: 3,
  },
  {
    field: 'payName',
    label: '支付方式',
    span: 3,
  },
  {
    field: 'isUpgrade',
    label: '升降级过卡种',
    span: 3,
    render: (curVal, data) => {
      let isUpgradeArray = [
        { label: '升降级过', value: 1 },
        { label: '没有升降级', value: 0 },
      ];
      const isUpgrade: any = isUpgradeArray.find(item => item.value == curVal);
      if (isUpgrade) {
        return render.renderTag(isUpgrade.label, 'red');
      }
      return '';
    },
  },
  {
    field: 'upgradePayAmount',
    label: '升级卡金额',
    span: 3,
    show: (data: any) => {
      return data.isUpgrade == 1;
    },
  },
  {
    field: 'initAmount',
    label: '电子金初始金额',
    span: 3,
  },
  {
    field: 'eleUseBalance',
    label: '电子金消费金额',
    span: 3,
    render: (curVal, data) => {
      return render.renderTag(curVal, 'red');
    },
  },
  {
    field: 'useVipAmount',
    label: '贵宾厅消费金额',
    span: 3,
    render: (curVal, data) => {
      let value = curVal + '元(消费金额为参考值，请以“核销记录”为准)';
      return render.renderTag(value, 'red');
    },
  },
  {
    field: 'canRefundOriginal',
    label: '原路退还',
    span: 3,
    render: (curVal, data) => {
      let canRefundOriginalArray = [
        { label: '支持', value: true },
        { label: '不支持', value: false },
      ];
      const canRefundOriginal: any = canRefundOriginalArray.find(item => item.value == curVal);
      if (canRefundOriginal) {
        return render.renderTag(canRefundOriginal.label, 'red');
      }
      return '';
    },
  },
];

async function handleSubmit() {
  try {
    const values = await validate();
    setDrawerProps({ confirmLoading: true });
    if (unref(isUpdate)) {
      await backcardSub(values);
    }
    closeDrawer();
    emit('success', false);
  } finally {
    setDrawerProps({ confirmLoading: false });
  }
}
</script>
